package com.lagou.edu.utils;

import java.sql.SQLException;

/**
 * 事务管理器类：负责手动事务的开启、提交、回滚
 * 通过connection控制事务,被ProxyFactory使用
 */
public class TransactionManager {

    /* 后面改为IoC注入
    private TransactionManager(){
    }
    private static TransactionManager transactionManager = new TransactionManager();
    public static TransactionManager getInstance() {
        return  transactionManager;
    }
    */

    private ConnectionUtils connectionUtils;

    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }

    // 开启手动事务控制
    public void beginTransaction() throws SQLException {
        connectionUtils.getCurrentThreadConn().setAutoCommit(false);
    }

    // 提交事务
    public void commit() throws SQLException {
        connectionUtils.getCurrentThreadConn().commit();
    }

    // 回滚事务
    public void rollback() throws SQLException {
        connectionUtils.getCurrentThreadConn().rollback();
    }
}
